var retainchannelCheckedItems;
var data_data;
function getretainchannelData() {
    
    
    
	var startDate = $("#retainchannelStartDate").val();
	var endDate = $("#retainchannelEndDate").val();
    
    
        
    var osID = document.getElementById("retainchannelOS").options[document.getElementById("retainchannelOS").selectedIndex].value;
    var typeID = document.getElementById("retainchannelType").options[document.getElementById("retainchannelType").selectedIndex].value;
    var datetype = document.getElementById("retainchanneldatetype").options[document.getElementById("retainchanneldatetype").selectedIndex].value;
    
    
    var f_id = document.getElementById("retainchannelf_id").options[document.getElementById("retainchannelf_id").selectedIndex].value; 
    //f_id = 276;
   
   

	$(".retainchannelChart").html("");
	if (startDate != "" && endDate != "")
	{
		var start = new Date();
		var end = new Date();

		//统计项
		var items = document.getElementsByName("retainchannelCheckbox");
		retainchannelCheckedItems = new Array();
		for(var i=0;i<items.length;i++) {
			if (items[i].checked)
			{
				retainchannelCheckedItems.push(items[i].value);
			}
		}

		if(retainchannelCheckedItems.length > 0) {


			jQuery('#body-wrapper').showLoading();
			start.setFullYear(startDate.substring(0,4), startDate.substring(5,7)-1, startDate.substring(8,10));
			end.setFullYear(endDate.substring(0,4), endDate.substring(5,7)-1 ,endDate.substring(8,10));

			days = parseInt((end - start)/1000/60/60/24)+1;

			var APIurl = "/client/retainchannel?staDate="+startDate+"&pd="+days;
	        
            
            if (osID != ""){
				APIurl += "&os="+osID;
            }
			if (typeID != ""){
				APIurl += "&dt="+typeID;
            }
            
            if (datetype != ""){
				APIurl += "&datetype="+datetype;
            }
            
            if (f_id != ""){
				APIurl += "&f_id="+f_id;
            }
            

			
			$.ajax({
				type: "GET",
				url: APIurl,
				dataType : "json",
				jsonnp: "callback",
				success:function(data,textStatus){ loadretainchannelData(data)}
			});
            
            
            $.ajax({
  url: APIurl,
  type: "POST",
  data: "username="+login_username,
success: function(msg){
   //alert('ok');
}
});
            
		}
		else {
			alert("请选择至少一个统计项");
		}

	}
	else {
		alert("请选择时间区域");
	}
}


var retainchannelTabArr, retainchannelDetailArr, retainchannelSumArr, retainchannelDateArr, MainSubTitle;
function loadretainchannelData(data) {
    
    var osID = document.getElementById("retainchannelOS").options[document.getElementById("retainchannelOS").selectedIndex].value;
    var typeID = document.getElementById("retainchannelType").options[document.getElementById("retainchannelType").selectedIndex].value;
    
    data_data = data;
	var tab = "";
	var startDate = $("#retainchannelStartDate").val();
	var endDate = $("#retainchannelEndDate").val();
	MainSubTitle = startDate.replace(/\//g, "-") + " 至 " + endDate.replace(/\//g, "-");
	jQuery('#body-wrapper').hideLoading();	


	retainchannelTabArr = new Array();
	retainchannelDetailArr = new Array();
	retainchannelSumArr = new Array();
	retainchannelDateArr = new Array();
    
    
    if(data['zongji'] != "0"){

			//显示选择Tab
			
            if(osID==""){

            genTabsretainchannel("retainchannel", "retainchannelOS", retainchannelTabArr, retainchannelDetailArr, retainchannelSumArr, retainchannelCheckedItems,0);
        }else if(typeID == ""){

            genTabsretainchannel("retainchannel", "retainchannelType", retainchannelTabArr, retainchannelDetailArr, retainchannelSumArr, retainchannelCheckedItems,0);
        }else{ genTabsretainchannel("retainchannel","", retainchannelTabArr, retainchannelDetailArr, retainchannelSumArr, retainchannelCheckedItems,0);  }
			
            
         
        changeChartType('retainchannel','Table');
        
        
        
        
        //生成表格
        genTop10Tableretainchannel("retainchannelTable", valueFullNameArr(retainchannelCheckedItems), retainchannelTabArr, data['All']['table'], retainchannelCheckedItems);

        
    }
    else{
        
    alert("选择的时间区间内无数据");
    
    }
	
}

function changeretainchannelTab(tabName,optionName) {
	
        
    
        //生成表格
        genTop10Tableretainchannel("retainchannelTable", valueFullNameArr(retainchannelCheckedItems), retainchannelTabArr, data_data[tabName]['table'], retainchannelCheckedItems);
        
    
    
    
	$(".bitem").removeClass("selbitem");
	$("#retainchannelTab"+tabName).addClass("selbitem");	
    
    
    
	
}



//改变渠道类型
function changeretainchannelType(obj) {
    
    var ftype = obj.options[obj.selectedIndex].value;
	
    
    $.ajax({
		type: "GET",
		url: "/client/getpnamecn",
        dataType: "html",
        data : {type : "retainchannel",ftype:ftype},
		success: function(msg){
            
            
            $("#pnamecnretainchannel").html(msg);
            
            
            
         
								}
				});
    
    
}



